package yf.mall.controller;

import java.util.*;

import com.yf.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;

import com.yf.consts.DaoConst;
import yf.mall.entity.CourseEntity;
import yf.mall.entity.SignEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.CourseService;
import yf.mall.service.NoticeService;
import yf.mall.service.SignService;
import com.yf.response.R;
import yf.mall.service.UserService;


/**
 * 报名Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Oct 21 08:39:58 CST 2019
 */
@RestController
@RequestMapping("/sign")
@Slf4j
public class SignController {
    @Autowired
    private SignService signService;

    /**
     * 列表报名
     */
    @RequestMapping("/list")
    @RequiresPermissions("sign:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageHelper.startPage(DaoConst.getPage(params), DaoConst.getLIMIT(params));
        List<SignEntity> signList = signService.queryList(params);
        return R.ok(new PageInfo(signList));
    }

    /**
     * 查看报名
     */
    @SysLog("查看报名")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sign:info")
    public R info(@PathVariable("id") Long id) {
        SignEntity sign = signService.get(id);

        return R.ok(sign);
    }

    /**
     * 保存报名
     */
    @SysLog("保存报名")
    @RequestMapping("/save")
    @RequiresPermissions("sign:save")
    public R save(SignEntity sign) {
        signService.save(sign);

        return R.ok(sign);
    }

    /**
     * 修改报名
     */
    @SysLog("修改报名")
    @RequestMapping("/update")
    @RequiresPermissions("sign:update")
    public R update(SignEntity sign) {
        //看是否要发送通知
        SignEntity signEntity = signService.get(sign.getId());
        CourseEntity courseEntity = courseService.get(sign.getCourseId());
        if (!signEntity.getActive() && sign.getActive()) {//未激活改到激活
            UserEntity userEntity = userService.get(signEntity.getUserId());
            noticeService.sendConfirmCourse(userEntity.getOpenId(), signEntity.getName(), courseEntity.getAddress() + DateUtil.dateToStr(courseEntity.getTime()));
        }
        signService.update(sign);

        return R.ok(sign);
    }

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private UserService userService;

    @Autowired
    private CourseService courseService;

    /**
     * 删除报名
     */
    @SysLog("删除报名")
    @RequestMapping("/delete")
    @RequiresPermissions("sign:delete")
    public R delete(Long[] ids) {
        signService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有报名
     */
    @SysLog("所有报名")
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SignEntity> signList = signService.queryList(params);

        return R.ok(signList);
    }

    /**
     @ModelAttribute public SearchParam<SignEntity> searchParam(SearchParam<SignEntity> params, SignEntity sign) {
     if (sign != null) {
     params.setEntity(sign);
     }

     return params;
     }
     **/
    /***********自定义开始***********/

    @SysLog("批量激活")
    @RequestMapping("/active")
    public R active(Long[] signIds) {
        signService.active(Arrays.asList(signIds));
        return R.ok();
    }

}
